package offer;

public class offerII_0053 {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode[]  list=new TreeNode[1];
        boolean[] flag=new boolean[1];
        flag[0]=false;
        search(root,flag,p,list);
        return list[0];
    }
    public void search(TreeNode node,boolean[] flag,TreeNode p,TreeNode[] list){
        if(node==null) return;
        search(node.left,flag,p,list);
        if(flag[0]) {
            list[0]=node;
            flag[0]=false;
        }
        if(node.val==p.val) flag[0]=true;
        search(node.right,flag,p,list);
    }
}
